package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhongyu.mapper.UserMapper;
import com.zhongyu.pojo.User;
import com.zhongyu.service.UserService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.UserVo;
import com.zhongyu.vo.UserVo1;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Resource
    private UserMapper userMapper;
   /* @Resource
    private AutoMapper autoMapper;*/

   /* @Override
    public List<User> searchByUserVo(UserVo userVo) {
      /*  QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),"user_code",userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,"user_role",userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,"birthday",userVo.getStartDate())
                .le(userVo.getEndDate()!=null,"birthday",userVo.getEndDate());
          return  super.list(queryWrapper);*/
    /*    LambdaQueryWrapper<User> lambdaQueryWrapper= Wrappers.<User> lambdaQuery();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
             return super.list(lambdaQueryWrapper);

    }*/

    @Override
    public List<User> searchAll() {
        return userMapper.selectList(null);
    }

    @Override
    public List<User> searchById(Integer id) {

        return  userMapper.selectById(id);
    }

    @Override
    public String searchById1(Integer userId) {
        return userMapper.selectById1(userId);
    }

    @Override
    public PageResult searchAll1(Integer pageNo, Integer pageSize, UserVo1 userVo1) {
        LambdaQueryWrapper<User> wrapper=Wrappers.<User>lambdaQuery();
        wrapper.like(!StringUtils.isEmpty(userVo1.getUserCode()),User::getUserCode,userVo1.getUserCode())
                .like(!StringUtils.isEmpty(userVo1.getUsername()), User::getUsername,userVo1.getUsername())
                .eq(userVo1.getGender()!=null,User::getGender,userVo1.getGender())
                .like(!StringUtils.isEmpty(userVo1.getEmail()), User::getEmail,userVo1.getEmail())
                .like(!StringUtils.isEmpty(userVo1.getPhone()), User::getPhone,userVo1.getPhone())
                .like(!StringUtils.isEmpty(userVo1.getAddress()), User::getAddress,userVo1.getAddress())
                .ge(userVo1.getStartDate()!=null,User::getBirthday,userVo1.getStartDate())
                .le(userVo1.getEndDate()!=null,User::getBirthday,userVo1.getEndDate())
                .eq(userVo1.getOrgId()!=null,User::getOrgId,userVo1.getOrgId());
        Page page=new Page(pageNo,pageSize);
        List<User> list=baseMapper.findAll1(page,wrapper);

        return new PageResult(list,page.getTotal());
    }

    @Override
    public void grant(Integer userId, Integer roleId) {

        baseMapper.update(userId, roleId);
    }



   /* @Override
    public PageResult searchbyUserVo(Integer pageNo, Integer pageSize, UserVo userVo) {
       // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<User> queryWrapper=Wrappers.<User>lambdaQuery();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        IPage<User> iPage= super.page(page,queryWrapper);
        return PageResult.getInstance(iPage);

    }
*/
 /* @Override
    public PageResult searchbyUserVo(Integer pageNo, Integer pageSize, UserVo userVo) {
       // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<User> queryWrapper=Wrappers.<User>lambdaQuery();
        queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        IPage<User> iPage= super.page(page,queryWrapper);

        PageResult pageResult= PageResult.getInstance(iPage);
        pageResult.getRows().forEach(autoMapper::mapperEntity);
        return pageResult;
    }*/
     /*public List<User> searchAll( UserVo userVo) {
         // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
         LambdaQueryWrapper<User> queryWrapper=Wrappers.<User>lambdaQuery();
         queryWrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                 .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                 .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                 .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
         return userMapper.findAll(queryWrapper);
     }*/

    public  PageResult searchAll(Integer pageNo, Integer pageSize, UserVo userVo) {
        // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<User> wrapper=Wrappers.<User>lambdaQuery();
        wrapper.like(!StringUtils.isEmpty(userVo.getUserCode()),User::getUserCode,userVo.getUserCode())
                .eq(userVo.getUserRole()!=null,User::getUserRole,userVo.getUserRole())
                .ge(userVo.getStartDate()!=null,User::getBirthday,userVo.getStartDate())
                .le(userVo.getEndDate()!=null,User::getBirthday,userVo.getEndDate());
        Page page=new Page(pageNo,pageSize);
        List<User> list=baseMapper.findAll(page,wrapper);
      /*  System.out.println("==>"+list.size());
        return PageResult.getInstanceByPage(page);*/
      return new PageResult(list,page.getTotal());
    }

    @Override
    public List<User> findByKgUser() {
        return userMapper.findByKgUser();
    }

    @Override
    public List<User> findByCgUser() {
        return userMapper.findByCgUser();
    }

    @Override
    public List<User> findByXsUser() {
        return userMapper.findByXsUser();
    }

    @Override
    public List<User> findByGLUser() {
        return userMapper.findByGLUser();
    }

    @Override
    public User searchByUserCode(String userCode) {
        LambdaQueryWrapper<User> wrapper=Wrappers.<User>lambdaQuery();
             wrapper.eq(User::getUserCode,userCode);
        return super.getOne(wrapper);
    }

    @Override
    public void add(User user) {
  userMapper.insert(user);
    }

    @Override
    public void del(Integer id) {
     userMapper.deleteById(id);
    }

    public  void  update(User user){
        userMapper.updateById(user);
    }
}
